package com.luyou.swing;

import com.luyou.utils.ResourcesUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;

/**
 * 创建窗口组件
 */
public class SwingComponent {

    JFrame f = new JFrame("测试");
    //定义一个按钮，并指定图标
    Icon okIcon = new ImageIcon(ResourcesUtils.getResourceFileAbsolutePath("ico/ok.png"));
    JButton ok = new JButton("确认", okIcon);

    //定义一个单选框
    JRadioButton male = new JRadioButton("男", true);
    JRadioButton female = new JRadioButton("女", false);
    ButtonGroup bg = new ButtonGroup();

    //定义一个复选框
    JCheckBox married = new JCheckBox("是否已婚？", false);

    //定义下拉框
    String[] colors = new String[]{"红色", "绿色", "蓝色"};
    JComboBox<String> colorChooser = new JComboBox<>(colors);

    //定义一个列表选择框
    JList<String> colorList = new JList<>(colors);

    //定义一个8行、20列的多行文本域
    JTextArea ta = new JTextArea(8, 20);

    //定义一个40列的单行文本域
    JTextField name = new JTextField(40);

    //定义菜单栏
    JMenuBar mb = new JMenuBar();
    JMenu file = new JMenu("文件");
    JMenu edit = new JMenu("编辑");

    //创建"新建"菜单项,并为之指定图标
    Icon newIcon = new ImageIcon(ResourcesUtils.getResourceFileAbsolutePath("ico/new.png"));
    JMenuItem newItem = new JMenuItem("新建",  newIcon);
    //创建"保存"菜单项,并为之指定图标
    Icon saveIcon = new ImageIcon(ResourcesUtils.getResourceFileAbsolutePath("ico/save.png"));
    JMenuItem saveItem = new JMenuItem("保存",  saveIcon);
    //创建"退出"菜单项,并为之指定图标
    Icon exitIcon = new ImageIcon(ResourcesUtils.getResourceFileAbsolutePath("ico/exit.png"));
    JMenuItem exitItem = new JMenuItem("退出",  exitIcon);

    JCheckBoxMenuItem autoWrap = new JCheckBoxMenuItem("自动换行");
    JMenuItem copyItem = new JMenuItem("复制",  new ImageIcon("/ico/copy.png"));
    JMenuItem pasteItem = new JMenuItem("粘贴",  new ImageIcon("/ico/paste.png"));
    JMenu format = new JMenu("格式");
    JMenuItem commentItem = new JMenuItem("注释");
    JMenuItem cancelItem = new JMenuItem("取消注释");

    //定义一个右键菜单用户设置程序风格
    JPopupMenu pop = new JPopupMenu();
    //用于组合3个风格菜单项的ButtonGroup
    ButtonGroup flavorGroup = new ButtonGroup();
    //创建5个单选按钮，s设置程序的外观风格
    JRadioButtonMenuItem metalItem = new JRadioButtonMenuItem("Metal 风格", true);
    JRadioButtonMenuItem nimbusItem = new JRadioButtonMenuItem("Nimbus 风格", true);
    JRadioButtonMenuItem windowsItem = new JRadioButtonMenuItem("Windows 风格", true);
    JRadioButtonMenuItem classicItem = new JRadioButtonMenuItem("Windows 经典风格", true);
    JRadioButtonMenuItem motifItem = new JRadioButtonMenuItem("Motif 风格", true);
    public void init(){
        JPanel bottom = new JPanel();
        bottom.add(name);
        bottom.add(ok);
        f.add(bottom, BorderLayout.SOUTH);

        JPanel checkPanel = new JPanel();
        checkPanel.add(colorChooser);
        bg.add(male);
        bg.add(female);
        checkPanel.add(male);
        checkPanel.add(female);
        checkPanel.add(married);
        //创建一个垂直排列组件的Box、盛装多行文本域JPanel
        Box topLef = Box.createVerticalBox();
        JScrollPane taJsp = new JScrollPane(ta);
        topLef.add(taJsp);
        topLef.add(checkPanel);
        Box top = Box.createHorizontalBox();
        top.add(topLef);
        top.add(colorList);
        f.add(top);

        //菜单
        //设置快捷键
        newItem.setAccelerator(KeyStroke.getKeyStroke('N', InputEvent.CTRL_DOWN_MASK));
        newItem.addActionListener(e -> ta.append("用户单击了“新建”菜单\n"));
        //为file才添加菜单项
        file.add(newItem);
        file.add(saveItem);
        file.add(exitItem);
        //为edit添加菜单项
        edit.add(autoWrap);
        edit.addSeparator();
        edit.add(copyItem);
        edit.add(pasteItem);
        //为commentItem组件提那家提示信息
        commentItem.setToolTipText("将程序代码注释起来！");
        format.add(commentItem);
        format.add(cancelItem);
        //swing支持这种增加分割符，只能用addSeparator
        edit.add(new JMenuItem("-"));
        edit.add(format);
        mb.add(file);
        mb.add(edit);
        f.setJMenuBar(mb);

        //右键菜单
        flavorGroup.add(metalItem);
        flavorGroup.add(nimbusItem);
        flavorGroup.add(windowsItem);
        flavorGroup.add(classicItem);
        flavorGroup.add(motifItem);
        pop.add(metalItem);
        pop.add(nimbusItem);
        pop.add(windowsItem);
        pop.add(classicItem);
        pop.add(motifItem);
        ActionListener flavorListener = e ->{
            try{
                switch (e.getActionCommand()){
                    case "Metal 风格":
                        changFlavor(1);
                        break;
                    case "Nimbus 风格":
                        changFlavor(2);
                        break;
                    case "Windows 风格":
                        changFlavor(3);
                        break;
                    case "Windows 经典风格":
                        changFlavor(4);
                        break;
                    case "Motif 风格":
                        changFlavor(5);
                        break;
                }
            }catch (Exception ex){
                ex.printStackTrace();
            }
        };
        metalItem.addActionListener(flavorListener);
        nimbusItem.addActionListener(flavorListener);
        windowsItem.addActionListener(flavorListener);
        classicItem.addActionListener(flavorListener);
        motifItem.addActionListener(flavorListener);
        //设置右键菜单
        ta.setComponentPopupMenu(pop);

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack();
        f.setVisible(true);
    }

    private void changFlavor(int flavor) throws Exception {

        switch (flavor){
            //设置Metal风格
            case 1:
                UIManager.setLookAndFeel(
                        "javax.swing.plaf.metal.MetalLookAndFeel"
                );
                break;
            case 2:
                UIManager.setLookAndFeel(
                        "javax.swing.plaf.nimbus.NimbusLookAndFeel"
                );
                break;
            case 3:
                UIManager.setLookAndFeel(
                        "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"
                );
                break;
            case 4:
                UIManager.setLookAndFeel(
                        "com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel"
                );
                break;
            case 5:
                UIManager.setLookAndFeel(
                        "com.sun.java.swing.plaf.motif.MotifLookAndFeel"
                );
                break;
        }
        //更新窗口内顶级容器以及内部所有组件的UI
        SwingUtilities.updateComponentTreeUI(f.getContentPane());
        //更新mb菜单条以及内部所有组件的UI
        SwingUtilities.updateComponentTreeUI(mb);
        //更新pop右键菜单内部所有组件的UI
        SwingUtilities.updateComponentTreeUI(pop);
    }


    public static void main(String[] args) {
        new SwingComponent().init();
    }
}
